package sample.event;

class EventSet {
	private Event[] events = new Event[100];
	private int next = 0;
	private int index = 0;

	public void add(Event e) {
		if (this.index >= this.events.length) {
			return;
		}
		this.events[index++] = e;
	}

	public Event getNext() {
		boolean looped = false;
		int start = this.next;
		do {
			this.next = (next + 1) % events.length;
			if (start == this.next)
				looped = true;
			if (this.next == (start + 1) % this.events.length && looped) {
				return this.events[this.next];
			}
		} while (this.events[this.next] == null);
		return this.events[this.next];
	}

	public void removeCurrent() {
		this.events[this.next] = null;
	}
}

public class Controller {
	private EventSet es = new EventSet();

	public void addEvent(Event c) {
		es.add(c);
	}

	public void run() {
		Event e;
		while ((e = this.es.getNext()) != null) {
			if (e.ready()) {
				e.action();
				System.out.println(e.description());
				this.es.removeCurrent();
			}
		}
	}

}
